package com.zengdada.authority.config.listener.jobs;

import com.zengdada.authority.config.listener.ApplicationReadyEventListener;
import com.zengdada.authority.config.quartz.BaseJob;
import com.zengdada.authority.config.quartz.JobAndTriggerManager;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2018/11/20 16:57
 * @Description: 清理容器内容任务
 */
@Slf4j
public abstract class ClearContainerJob implements BaseJob {
    @Autowired
    JobAndTriggerManager jobAndTriggerManager;

    public abstract void execute(String key, Object value);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String key = null, jobName = null;
        try {
            key = (String) jobExecutionContext.getJobDetail().getJobDataMap().get(ApplicationReadyEventListener.CONTENT_KEY);
            jobName = (String) jobExecutionContext.getJobDetail().getJobDataMap().get(ApplicationReadyEventListener.JOB_NAME);
            log.debug("清理容器中key为：{} 的内容。", key);
            ApplicationReadyEventListener.removeContainerContent(key);
            execute(key, ApplicationReadyEventListener.getContainerContent(key));
        } catch (Exception e) {
            log.error("清理容器中key为：{} 的内容失败，{}", key, e.getMessage());
        } finally {
            try {
                jobAndTriggerManager.deleteJob(jobName, ApplicationReadyEventListener.CONTENT_KEY);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }
}
